package com.yxkj.syh.app.huarong.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.syh.app.basic.utils.imageview.GlideCircleWithBorder;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.AuthImageData;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/26 15:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class AuthAdp extends RecyclerView.Adapter<AuthAdp.ViewHolder> {
    List<AuthImageData> data = new ArrayList<>();
    private OnClick mOnClick;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_auth_data, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AuthImageData authImageData = data.get(position);
        if (authImageData.getStatus() == 0) {
            holder.ivCamera.setVisibility(View.VISIBLE);
            if (mOnClick != null) {
                holder.ivCamera.setOnClickListener(v -> {
                    mOnClick.onCameraClick(authImageData);
                });
            }
        } else {
            holder.ivCamera.setVisibility(View.GONE);
        }
        if (authImageData.isExp()) {
            holder.tvExp.setVisibility(View.VISIBLE);
            if (mOnClick != null) {
                holder.tvExp.setOnClickListener(v -> {
                    mOnClick.oneExpClick(authImageData);
                });
            }
        } else {
            holder.tvExp.setVisibility(View.GONE);
        }
        Glide.with(holder.itemView.getContext()).load(authImageData.getIamgeUrl()).apply(RequestOptions.errorOf(authImageData.getDefRec())
                .diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(holder.ivImage);
        holder.tvTitle.setText(authImageData.getTitle());

    }

    public void setNewData(List<AuthImageData> authImageData) {
        data.clear();
        data.addAll(authImageData);
        notifyDataSetChanged();
    }

    public List<AuthImageData> getData() {
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnClick(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage, ivCamera;
        TextView tvTitle,tvExp;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.ivImage);
            ivCamera = itemView.findViewById(R.id.ivCamera);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvExp = itemView.findViewById(R.id.tvExp);
        }
    }

    public interface OnClick {
        void onCameraClick(AuthImageData authImageData);
        void oneExpClick(AuthImageData authImageData);
    }

}
